<?php

$page = $_GET['page']; // get the requested page 
$limit = $_GET['rows']; // get how many rows we want to have into the grid 
$sidx = $_GET['sidx']; // get index row - i.e. user click to sort 
$sord = $_GET['sord']; // get the direction 

$admin = true;
require_once('auth_req.inc.php');

$rs = $dbh->query('select count(*) as count from events');

if ($rec = $rs->fetchAll(PDO::FETCH_COLUMN, 0))
{
    $count = $rec[0];
}
else
{
    $count = 0;
}

if( $count > 0 ) 
{ 
    $total_pages = ceil($count/$limit); 
} 
else 
{ 
    $total_pages = 0; 
} 

if ($page > $total_pages) 
    $page=$total_pages; 

$start = $limit * $page - $limit; // do not put $limit*($page - 1) 

if ($start < 0) 
    $start = 0;

$sth = $dbh->prepare('select * from events order by '. $sidx . ' ' . $sord  .' limit :start,:limit');

$sth->bindValue(':start', intval($start), PDO::PARAM_INT);
$sth->bindValue(':limit', intval($limit), PDO::PARAM_INT);

$rs = $sth->execute();

$response = new stdClass();

$response->page = $page; 
$response->total = $total_pages; 
$response->records = $count; 

$alldb = $sth->fetchAll(PDO::FETCH_ASSOC); 

foreach( $alldb as $key => $val)
{
    $response->rows[$key]['id'] = $val['e_id'];
    $response->rows[$key]['cell'] = array($val['name']);
}

echo json_encode($response);

?>
